﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ZaxLib.Web.UI.WebControls
{
	[
	DefaultProperty("Text"),
	ToolboxData("<{0}:GridHeader runat=server></{0}:GridHeader>"),
	ParseChildren(true, "GridItems")
	]
	public class GridData : WebControl, IEnumerable, IGridItem
	{
		#region Class Parameters

		private int _idx;
		private ArrayList _gridItems;
		private Dictionary<String, Object> _data = null;

		#endregion

		#region Tag Parameters

		[Bindable(true)]
		[Category("Appearance")]
		[DefaultValue("")]
		[Localizable(true)]
		public TableRow Data
		{
			get
			{
				TableRow row = ViewState["HeadData"] as TableRow;
				if (row == null)
				{
					row = new TableRow();
					Data = row;
					BuildRow();
				}
				return row;
			}

			set
			{
				ViewState["HeadData"] = value;
			}
		}

		[
			Category("Behavior"),
			Description("Element in the grid to be drawn"),
			DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
			PersistenceMode(PersistenceMode.InnerDefaultProperty)
		]
		public ArrayList GridItems
		{
			get
			{
				if (_gridItems == null)
				{
					_gridItems = new ArrayList();
				}

				return _gridItems;
			}
			set
			{
				_gridItems = value;
			}
		}

		#endregion

		#region Render Methods

		public override void RenderControl(HtmlTextWriter writer)
		{
			if (Data != null)
			{
				Data.CssClass = CssClass;
				Data.RenderControl(writer);
			}
		}

		protected void BuildRow()
		{
			if (_data != null && _data.Count > 0)
			{
				foreach (GridItem item in GridItems)
				{
					if (_data.ContainsKey(item.Field))
					{
						TableCell td = new TableCell();
						// TODO: Figure out how to provide a UniqueID to this cell
						//td.ID = String.Format("{0}{1}", UniqueID, item.Field);
						td.Text = _data[item.Field].ToString();
						Data.Controls.Add(td);
					}
				}
			}
		}

		#endregion

		#region Enumerator Methods

		private GridEnumerator _enumerator = null;

		IEnumerator IEnumerable.GetEnumerator()
		{
			if (_enumerator == null) _enumerator = new GridEnumerator(this);
			return _enumerator;
		}

		#endregion

		#region GridItem Interface Methods

		public bool Next()
		{
			bool retVal = false;
			if (Index + 1 < Data.Cells.Count)
			{
				retVal = true;
				Index++;
			}

			return retVal;
		}

		public int Index
		{
			get
			{
				return _idx;
			}
			set
			{
				_idx = value;
			}
		}

		public void Reset()
		{
			Index = -1;
		}

		public Object Current
		{
			get
			{
				return Data.Cells[Index];
			}
		}

		public void SetData(Dictionary<String, Object> inData)
		{
			// Set current row data
			_data = inData;
			// Create a new Table row item
			Data = new TableRow();
			// Build row
			BuildRow();
		}

		#endregion
	}
}
